/*
 * Copyright (c) Scrap Material Junior Brother, Xin. 2023-2023. All rights reserved.
 */

package string;

import util.BaseUtils;

import java.util.Arrays;

/**
 * 字符串常用API
 *
 * @author xin
 * @since 2023-05-01
 */
public class StringApi {
    private static void declare() {
        String str1 = "abc";
        String str2 = new String("abc");
        char[] chars = {'a', 'b', 'c'};
        String str3 = new String(chars);
        System.out.printf("str1: %s, str2: %s, str3: %s\n", str1, str2, str3);
        System.out.printf("str1: %s, str1.length(): %d\n", str1, str1.length());
    }

    private static void compare() {
        String str1 = "abc";
        String str2 = "abc";
        String str3 = new String("abc");
        String str4 = "abC";
        StringBuilder sb = new StringBuilder("abc");
        // ==
        System.out.printf("str1: %s, str2: %s, str1 == str2: %s\n", str1, str2, str1 == str2);
        System.out.printf("str1: %s, str3: %s, str1 == str3: %s\n", str1, str3, str1 == str3);
        // equal
        System.out.printf("str1: %s, str2: %s, str1.equal(str2): %s\n", str1, str2, str1.equals(str2));
        System.out.printf("str1: %s, str3: %s, str1.equal(str3): %s\n", str1, str3, str1.equals(str3));
        // equalsIgnoreCase
        System.out.printf("str1: %s, str4: %s, str1.equal(str4): %s\n", str1, str4, str1.equals(str4));
        System.out.printf("str1: %s, str4: %s, str1.equalsIgnoreCase(str4): %s\n", str1, str4, str1.equalsIgnoreCase(str4));
        // contentEquals
        System.out.printf("str1: %s, sb: %s, str1.equals(sb): %s\n", str1, sb, str1.equals(sb));
        System.out.printf("str1: %s, sb: %s, str1.equals(sb.toString()): %s\n", str1, sb, str1.equals(sb.toString()));
        System.out.printf("str1: %s, sb: %s, str1.contentEquals(sb): %s\n", str1, sb, str1.contentEquals(sb));
        System.out.printf("str3: %s, sb: %s, str3.contentEquals(sb): %s\n", str3, sb, str3.contentEquals(sb));

        // compare
        String str5 = "hello";
        String str6 = "help";
        String str7 = "help me";
        String str8 = "HELP ME";
        // 比较同位字母大小
        System.out.printf("str5: %s, str6: %s, str5.compareTo(str6): %s\n", str5, str6, str5.compareTo(str6));
        // 比较长度
        System.out.printf("str6: %s, str7: %s, str6.compareTo(str7): %s\n", str6, str7, str6.compareTo(str7));
        // 不区分字母大小
        System.out.printf("str7: %s, str8: %s, str7.compareToIgnoreCase(str8): %s\n", str7, str8, str7.compareToIgnoreCase(str8));
    }

    private static void find() {
        String str = "hello";
        System.out.printf("str: %s, str.charAt(0): %s\n", str, str.charAt(0));
        System.out.printf("str: %s, str.indexOf(\"h\"): %s\n", str, str.indexOf("h"));
        System.out.printf("str: %s, str.indexOf(\"he\"): %s\n", str, str.indexOf("he"));
        System.out.printf("str: %s, str.indexOf(\"l\"): %s\n", str, str.indexOf("l"));
        System.out.printf("str: %s, str.indexOf(\"l\", 1): %s\n", str, str.indexOf("l", 1));
        System.out.printf("str: %s, str.lastIndexOf(\"l\"): %s\n", str, str.lastIndexOf("l"));
        System.out.printf("str: %s, str.contains(\"he\"): %s\n", str, str.contains("he"));
        System.out.printf("str: %s, str.startsWith(\"he\"): %s\n", str, str.startsWith("he"));
        System.out.printf("str: %s, str.startsWith(\"e\", 1): %s\n", str, str.startsWith("e", 1));
        System.out.printf("str: %s, str.endsWith(\"o\"): %s\n", str, str.endsWith("o"));
        System.out.printf("str: %s, str.regionMatches(2, str, 3, 1): %s\n", str, str.regionMatches(2, str, 3, 1));
    }

    private static void transform() {
        String str = "1234";
        System.out.printf("str: %s, Integer.parseInt(str): %s\n", str, Integer.parseInt(str));
        System.out.printf("str: %s, Arrays.toString(str.toCharArray()): %s\n", str, Arrays.toString(str.toCharArray()));

        str = "abcDEF";
        System.out.printf("str: %s, str.toLowerCase(): %s\n", str, str.toLowerCase());
        System.out.printf("str: %s, str.toUpperCase(): %s\n", str, str.toUpperCase());
    }

    private static void replace() {
        String str = "12355";
        System.out.printf("str: %s, str.replace(\"5\", \"4\"): %s\n", str, str.replace("5", "4"));
        System.out.printf("str: %s, str.replaceFirst(\"5\", \"4\"): %s\n", str, str.replaceFirst("5", "4"));
    }

    private static void split() {
        String str = "123,456,789";
        String[] arr = str.split(",");
        System.out.printf("str: %s, Arrays.toString(arr): %s\n", str, Arrays.toString(arr));
    }

    private static void substring() {
        String str = "010-12345";
        System.out.printf("str: %s, str.substring(3): %s\n", str, str.substring(3));
        System.out.printf("str: %s, str.substring(3, 7): %s\n", str, str.substring(3, 7));

        str = " 1 2 3 ";
        System.out.printf("str: '%s', str.trim(): '%s'\n", str, str.trim());
    }

    private static void concat() {
        String str1 = "abc";
        String str2 = "def";
        String[] arr = {"abc", "def"};
        System.out.printf("str1: %s, str2: %s, str1 + str2: %s\n", str1, str2, str1 + str2);
        System.out.printf("str1: %s, str2: %s, String.join(\"\", str1, str2): %s\n", str1, str2, String.join("", str1, str2));
        System.out.printf("arr: %s, String.join(\"\", arr): %s\n", Arrays.toString(arr), String.join("", arr));
    }

    private static void reverse() {
        String str = "abc";
        StringBuilder sb = new StringBuilder(str);
        sb.reverse();
        System.out.printf("str: %s, reverse: %s\n", str, sb);
    }

    private static void delete() {
        String str = "abdec";
        StringBuilder sb = new StringBuilder(str);
        sb.delete(2, 4);
        System.out.printf("str: %s, sb: %s\n", str, sb);
    }

    private static void insert() {
        String str = "ac";
        StringBuilder sb = new StringBuilder(str);
        sb.insert(1, "b", 0, 1);
        System.out.printf("str: %s, sb: %s\n", str, sb);
    }

    private static void replaceDetail() {
        String str = "adc";
        StringBuilder sb = new StringBuilder(str);
        sb.replace(1, 2, "b");
        System.out.printf("str: %s, sb: %s\n", str, sb);
    }

    private static void copyValueOf() {
        System.out.printf("%s\n", String.copyValueOf(new char[] {'a', 'b', 'c', 'd', 'e', 'f'}, 0, 3));
        System.out.printf("%s\n", String.copyValueOf("abcdef".toCharArray(), 0, 3));
    }

    public static void main(String[] args) {
        BaseUtils.print("declare", StringApi::declare);
        BaseUtils.print("compare", StringApi::compare);
        BaseUtils.print("find", StringApi::find);
        BaseUtils.print("transform", StringApi::transform);
        BaseUtils.print("replace", StringApi::replace);
        BaseUtils.print("split", StringApi::split);
        BaseUtils.print("substring", StringApi::substring);
        BaseUtils.print("concat", StringApi::concat);
        BaseUtils.print("reverse", StringApi::reverse);
        BaseUtils.print("delete", StringApi::delete);
        BaseUtils.print("insert", StringApi::insert);
        BaseUtils.print("replaceDetail", StringApi::replaceDetail);
        BaseUtils.print("copyValueOf", StringApi::copyValueOf);
    }
}
